dojo.provide("apt.FilterPopover");
dojo.require("apt.Popover");

dojo.declare("apt.FilterPopover", [apt.Popover], {
    templateString: dojo.cache('apt.templates', 'FilterPopover.html'),
    start: '',
    end: '',
    license: '',
    contact: '',
    parent: '',

    filterResourcesId: 'filterResourcesId',
    filterShiftsId: 'filterShiftsId',
    filterDepartmentsId: 'filterDepartmentsId',
    filterLicenseNumberId: 'filterLicenseNumberId',

    constructor: function (data) {
        this.inherited(arguments);
    },

    postCreate: function () {
        var b = dojo.query('body')[0];
        this.placeAt(b, 'last');

        // Generate options for Departments
        dojo.place(this.generateDepartmentOptions(), this.departmentsNode);

        // Generate options for Shifts
        //dojo.place(this.generateShiftOptions(), this.shiftsNode);
        // this.licenseNumberNode --> get licenseNumber
        //console.log(this.licenseNumberNode);

        // Generate options for resources
        dojo.place(this.generateResourceOptions(), this.resourcesNode);

    },

    generateOption: function (value, label) {
        return '<option value="' + value + '">' + label + '</option>\n';
    },

    generateResourceOptions: function () {
        //debugger;
        var resources = this.parent.stores.resources.query();
        var options = this.generateOption('0', 'All');  // *
        for (var i = 0; i < resources.length; i++) {
            options = options + this.generateOption(resources[i]['SmanId'], resources[i]['Name']);
        }
        ;
        return dojo.create('select', {innerHTML: options, id: this.filterResourcesId});
    },

    generateShiftOptions: function () {
        var shifts = this.parent.stores.shifts.query();
        var options = this.generateOption('0', 'All');  // *

        for (var i = 0; i < shifts.length; i++) {
            options = options + this.generateOption(shifts[i]['ShiftID'], shifts[i]['Description'] + " " + shifts[i]['StartTime'] + " - " + shifts[i]['EndTime']);
        }
        ;
        return dojo.create('select', {innerHTML: options, id: this.filterShiftsId});
    },

    generateDepartmentOptions: function () {
        var departments = this.parent.stores.departments.query();
        var options = this.generateOption('0', 'All');  // *

        for (var i = 0; i < departments.length; i++) {
            options = options + this.generateOption(departments[i]['Code'], departments[i]['Description']);
        }
        ;
        return dojo.create('select', {innerHTML: options, id: this.filterDepartmentsId});
    },

    filterData: function (e) {
        var filter = {
            licno: dojo.byId('filterLicenseNumberId').value,
            smanid: dojo.byId(this.filterResourcesId).value,
            deptid: dojo.byId(this.filterDepartmentsId).value,
            daysShowing: this.parent.daysShowing
        };
        var location = window.location;
        var filterObj = dojo.queryToObject(location.search.split("?").join(""));
        var queryStr = dojo.mixin(filterObj, filter);
        var params = dojo.objectToQuery(queryStr);
        var redirect2 = location.origin + location.pathname + "?" + params;
        //console.log(redirect2);
        self.location = redirect2;
        dojo.stopEvent(e);
        dojo.style(this.domNode, 'display', 'none');
    },

    show: function(){
        var search = window.location.search.split("?").join(""); //str.split(search).join(replacement)
        var filterObj = dojo.queryToObject(search);
        filterObj.deptid && dojo.attr(this.filterDepartmentsId, 'value', filterObj.deptid);
        filterObj.smanid && dojo.attr(dojo.byId(this.filterResourcesId), 'value', filterObj.smanid);
        filterObj.licno && dojo.attr(dojo.byId(this.filterLicenseNumberId), 'value', filterObj.licno);
        dojo.style(this.domNode, 'display', 'block');
    },

    toggle: function () {
    },

    hide: function (e) {
        dojo.stopEvent(e);
        dojo.style(this.domNode, 'display', 'none');
    }

});
